﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using Yoozoo.Mars.Got;

namespace Yoozoo.Core.Common
{
    public class VolumeQualityListener : QualityListenerMono,
        IPostEffectAdjuster
    {
        [Serializable]
        public class VolumeStyleSetting
        {
            public int style = 0;

            public Volume Volume;

            public void Enable()
            {
                if (Volume == null)
                {
                    //UnityEngine.Debug.LogError("无法开启后效，没有赋值Volume");
                    return;
                }

                if (!Volume.enabled)
                {
                    Volume.enabled = true;
                }
            }

            public void Disable()
            {
                if (Volume == null)
                {
                    return;
                }
                Volume.enabled = false;
            }
        }

        public List<VolumeStyleSetting> styleSettings;

        protected void Awake()
        {
            foreach (var setting in styleSettings)
            {
                setting.Disable();
            }
        }

        public void ChangePostEffectStyle(int style)
        {
            foreach (var setting in styleSettings)
            {
                setting.Disable();
            }

            foreach (var setting in styleSettings)
            {
                if (setting.style == style)
                {
                    needUpdateVolumeStyleSetting = setting;
                    // Timers.inst.CallLater((o)=>
                    // {
                        setting.Enable();
                        // });
                        /*setting.Enable();
                        if (!hasLateUpdated)
                        {
                            needUpdateVolumeStyleSetting = setting;
                            needLateUpdate = true;
                        }*/
                        break;

                }
            }

        }

        /// <summary>
        /// 设置Bloom效果是否开启
        /// </summary>
        /// <param name="enable"></param>
        public void EnableBloom(bool enable)
        {
            if (needUpdateVolumeStyleSetting == null)
            {
                return;
            }

            if (needUpdateVolumeStyleSetting.Volume.sharedProfile.TryGet<Bloom>(out var bloom))
            {
                bloom.active = enable;
                VolumeManager.instance.SetUpdateOverrideDataState(UpdateOverrideDataType.Semi_Auto);
            }
        }

        private VolumeStyleSetting needUpdateVolumeStyleSetting;
        private bool needLateUpdate = false;

        private bool hasLateUpdated = false;

        /*private void Update()
        {
            hasLateUpdated = false;
        }


        private void LateUpdate()
        {
            if (needLateUpdate)
            {
                if (needUpdateVolumeStyleSetting != null)
                {
                    needUpdateVolumeStyleSetting.Disable();
                    needUpdateVolumeStyleSetting.Enable();
                    needUpdateVolumeStyleSetting = null;
                }
                needLateUpdate = false;
            }
            hasLateUpdated = true;
        }*/

        public void ChangePostEffectLevel(int level)
        {

        }
    }
}
